package com.mission.schedule.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mission.schedule.R;
import com.mission.schedule.bean.repeat607.RepeatRecommendContent;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.Utils;
import com.mission.schedule.widget.SwipeLinearLayout;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RepeatFriendsRiChengAdapter extends BaseAdapter {

    Context context;
    int mScreenWidth;
    List<RepeatRecommendContent.PageBean.ItemsBean> mList;
    private LayoutInflater mInflater;
    public static Map<Integer, Boolean> mChecked;
    Handler handler;
    boolean isnew = false;

    public RepeatFriendsRiChengAdapter(Context context, List<RepeatRecommendContent.PageBean.ItemsBean> mList,
                                       int mScreenWidth, Handler handler, boolean isnew) {
        this.context = context;
        this.mList = mList;
        mInflater = LayoutInflater.from(context);
        this.mScreenWidth = mScreenWidth;
        this.handler = handler;
        this.isnew = isnew;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    List<SwipeLinearLayout> swipeLinearLayouts = new ArrayList<>();

    public void remove(int i){
        mList.remove(i);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        RepeatRecommendContent.PageBean.ItemsBean bean = mList.get(position);
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_repeatfriendsricheng,
                    null);

            viewHolder = new ViewHolder();
            viewHolder.date_ll = (RelativeLayout) convertView
                    .findViewById(R.id.date_ll);
            viewHolder.date_tv = (TextView) convertView
                    .findViewById(R.id.date_tv);
            viewHolder.time_tv = (TextView) convertView
                    .findViewById(R.id.time_tv);
            viewHolder.content_tv = (TextView) convertView.findViewById(R.id.content_tv);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.before = (TextView) convertView.findViewById(R.id.beforetime);
            viewHolder.swipeLayout = (SwipeLinearLayout) convertView.findViewById(R.id.content_rl);
            swipeLinearLayouts.add(viewHolder.swipeLayout);
            viewHolder.more = (LinearLayout) convertView.findViewById(R.id.delete_ll);// 更多
            viewHolder.c_ll = (LinearLayout) convertView.findViewById(R.id.c_ll);
            viewHolder.seven_ll = (LinearLayout) convertView.findViewById(R.id.seven_ll);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISADMIN, "0").equals("0")) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    Utils.dipTopx(context, 0), RelativeLayout.LayoutParams.MATCH_PARENT);
            viewHolder.more.setLayoutParams(params);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    Utils.dipTopx(context, 90), RelativeLayout.LayoutParams.MATCH_PARENT);
            params.setMargins(0,Utils.dipTopx(context,10),0,0);
            viewHolder.more.setLayoutParams(params);
        }
        viewHolder.swipeLayout.setOnSwipeListener(new SwipeLinearLayout.OnSwipeListener() {
            @Override
            public void onDirectionJudged(SwipeLinearLayout thisSll, boolean isHorizontal) {
                if (false == isHorizontal) {
                    for (SwipeLinearLayout sll : swipeLinearLayouts) {
                        if (null == sll) {
                            continue;
                        }
                        sll.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
                    }
                } else {
                    for (SwipeLinearLayout sll : swipeLinearLayouts) {
                        if (null == sll) {
                            continue;
                        }
                        if (!sll.equals(thisSll)) {
                            //划开一个sll， 其他收缩
                            sll.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
                        }
                    }
                }
            }
        });
        viewHolder.c_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = 1;
                message.arg1 = position;
                handler.sendMessage(message);
            }
        });
        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.swipeLayout.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
                //右滑删除
                Message message = new Message();
                message.what = 2;
                message.arg1 = position;
                handler.sendMessage(message);
            }
        });
        String strdate = bean.repTypeParameter.replace("[", "").replace("]", "").replace("\"", "").toString();
        String weekStr;
        if (bean.repType == 1) {
            viewHolder.date_tv.setText("每天");
            viewHolder.time_tv.setText(isnew ? "每天 " + bean.repTime : bean.repTime);
        } else if (bean.repType == 2) {
            viewHolder.date_tv.setText("每周");
            if (strdate.equals("1")) {
                weekStr = "一";
            } else if (strdate.equals("2")) {
                weekStr = "二";
            } else if (strdate.equals("3")) {
                weekStr = "三";
            } else if (strdate.equals("4")) {
                weekStr = "四";
            } else if (strdate.equals("5")) {
                weekStr = "五";
            } else if (strdate.equals("6")) {
                weekStr = "六";
            } else {
                weekStr = "日";
            }
            viewHolder.time_tv.setText((isnew ? "每周" : "周") + weekStr + " " + bean.repTime);
        } else if (bean.repType == 3) {
            viewHolder.date_tv.setText("每月");
            viewHolder.time_tv.setText(isnew ? ("每月" + strdate + "号 " + bean.repTime) : (strdate + "日" + " " + bean.repTime));
        } else if (bean.repType == 4) {
            viewHolder.date_tv.setText("每年");
            viewHolder.time_tv.setText(isnew ? ("每年" + strdate.replace("-", "月") + "号 " + bean.repTime) : (strdate + " " + bean.repTime));
        } else if (bean.repType == 6) {
            viewHolder.date_tv.setText("每年");
            viewHolder.time_tv.setText(isnew ? ("每年" + strdate.replace("-", "月") + " " + bean.repTime) : (strdate + " " + bean.repTime));
        } else {
            viewHolder.date_tv.setText("工作日");
            viewHolder.time_tv.setText(isnew ? "工作日 " + bean.repTime : bean.repTime);
        }
        viewHolder.content_tv.setText(bean.repContent);

        if (isnew) {
            viewHolder.seven_ll.setVisibility(View.VISIBLE);
            viewHolder.date_ll.setVisibility(View.GONE);
            viewHolder.name.setText("@" + bean.recommendName);
            Date createTime = DateUtil.parseDateTimeSs(bean.createTime.replace('T', ' '));
            Date dateToday = DateUtil.parseDateTime(DateUtil.formatDateTimeSs(new Date()));
            long betweem = (long) (createTime.getTime() - dateToday.getTime()) / 1000;
            long day = betweem / (24 * 3600);
            long hour = betweem % (24 * 3600) / 3600;
            long min = betweem % 3600 / 60;
            if (Math.abs(day) >= 1) {
                viewHolder.before.setText(Math.abs(day) + "天前");
            } else {
                if (Math.abs(hour) >= 1) {
                    viewHolder.before.setText(Math.abs(hour) + "小时前");
                } else {
                    viewHolder.before.setText(Math.abs(min) + "分钟前");
                }
            }
        } else {
            viewHolder.seven_ll.setVisibility(View.GONE);
            if (position == 0) {
                viewHolder.date_ll.setVisibility(View.VISIBLE);
            } else if (bean.repType == mList.get(position - 1).repType) {
                viewHolder.date_ll.setVisibility(View.GONE);
            } else if (bean.repType == 4 && mList.get(position - 1).repType == 6) {
                viewHolder.date_ll.setVisibility(View.GONE);
            } else if (bean.repType == 6 && mList.get(position - 1).repType == 4) {
                viewHolder.date_ll.setVisibility(View.GONE);
            } else {
                viewHolder.date_ll.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    static class ViewHolder {
        public RelativeLayout date_ll;
        public TextView date_tv;
        public TextView time_tv;
        public TextView content_tv, name, before;
        SwipeLinearLayout swipeLayout;
        LinearLayout more, c_ll, seven_ll;
    }

}
